﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Reports.ReportCoordinationPrices
{
    public partial class PrintForm : Form,IntegrationInterface.IDIProjectForm,IntegrationInterface.IDIReport
    {
        public PrintForm()
        {
            InitializeComponent();
        }
       // private Int32 _docId = 6201;

        public Int32 DocId
        {
            get 
            {
                if (!string.IsNullOrEmpty(Login))
                    return Convert.ToInt32(Login);
                return 0;
            }
            
        }
        #region IDIProjectForm Members
        private string _conStr;
        public string ConnectionString
        {
            get
            {
                return _conStr;
            }
            set
            {
                _conStr = value;
            }
        }
        private string _log;
        public string Login
        {
            get
            {
                return _log;
            }
            set
            {
                _log = value;
            }
        }

        public Form MailForm
        {
            get 
            {
                return this;
            }
        }
        private object _obj;
        public object CommonProperty
        {
            get
            {
                return _obj;
            }
            set
            {
                _obj = value;
            }
        }

        #endregion

        #region IDIReport Members

        public void InitReport(string reportPath)
        {
            Reports.ReportCoordinationPrices.DataSet ds = new Reports.ReportCoordinationPrices.DataSet();
            Reports.ReportCoordinationPrices.DataLoader.GetData(ds.Data, this.ConnectionString, this.DocId);
            CalculateData(ds.Data);
            Reports.ReportCoordinationPrices.DataLoader.GetInfo(ds.Info, this.ConnectionString, this.DocId);
            CrystalReport1.FileName = reportPath;
            CrystalReport1.Load();
            CrystalReport1.SetDataSource(ds);
        }
        private void CalculateData(Reports.ReportCoordinationPrices.DataSet.DataDataTable data)
        {
            foreach(Reports.ReportCoordinationPrices.DataSet.DataRow dr in data)
            {
                byte nds = 0;
                decimal tmp = 0;
                if (!dr.IsNDSTaxNull())
                {
                    nds = dr.NDSTax;
                }                
                decimal manPrice = 0;
                if (!dr.IsManufacturerPriceNoNDSNull())
                {
                    manPrice = dr.ManufacturerPriceNoNDS;
                }
                double imprtMarkup = 0;
                if (!dr.IsImportMarkupNull())
                {
                    imprtMarkup = dr.ImportMarkup;
                    tmp = Convert.ToDecimal(manPrice * (Convert.ToDecimal(imprtMarkup / 100)));//сумма торговой надбавки импортера
                    dr.ImportMarkupSum = tmp;
                    tmp = 0;
                }
                double optMarkup = 0;
                if (!dr.IsOptMarkupNull())
                {
                    optMarkup = dr.OptMarkup;
                    tmp = Convert.ToDecimal(manPrice * (Convert.ToDecimal(optMarkup / 100)));//сумма торговой надбавки оптового звена
                    dr.OptMarkupSum = tmp;
                }
                dr.AllMarkup = imprtMarkup + optMarkup;
                if (dr.AllMarkup > 0)
                {
                    tmp = Convert.ToDecimal(manPrice * Convert.ToDecimal(dr.AllMarkup / 100));
                    dr.AllMarkupSum = tmp;
                    tmp = 0;
                }
                decimal supPrice = 0;
                if (!dr.IsOptWithNDSNull())
                {
                    tmp = dr.OptWithNDS * Convert.ToDecimal( 100.00 / (100.00 + nds));
                    dr.OptNDS = dr.OptWithNDS - tmp;
                    dr.OptNoNDS = tmp;
                    tmp = 0;
                }
                if (!dr.IsRetailMarkupPrcNull())
                {
                    tmp = supPrice * Convert.ToDecimal(dr.RetailMarkupPrc / 100);
                    dr.RetailMarkupSum = tmp;
                    tmp = 0;
                }
                if (!dr.IsRetailPriceWithNDSNull())
                {
                    tmp = dr.RetailPriceWithNDS * Convert.ToDecimal(100.00 / (100.00 + nds));
                    dr.RetailPriceNoNDS = tmp;
                    dr.RetailPriceNDSValue = dr.RetailPriceWithNDS - tmp;
                }
                if (!dr.IsRetailPriceNoNDSNull() && !dr.IsOptNoNDSNull())
                {
                    if ((dr.OptNoNDS > 0) && (dr.RetailPriceNoNDS > 0))
                    {
                        dr.RetailMarkupSum = dr.RetailPriceNoNDS - dr.OptNoNDS;
                    }
                }
            }
        }

        #endregion
    }
}